#!/bin/bash

set -o errexit
export LC_ALL=C

export TMP_DATA_PATH=/tmp/mixagol/annotation_cluster

export BASE_DATA_PATH=/home/mixagol/work/data
# export BASE_DATE=20111030
export BASE_DATE=20120802

export GENOME_ID=$1
export AN_DIR=~/work/data/annotation_cluster/${GENOME_ID}

if [ -d ${AN_DIR} ]; then
    echo "Directory for annotation genome ${GENOME_ID} exists!" > /dev/stderr
    exit 1
fi

mkdir -p ${AN_DIR}/orgs

hostname > ${AN_DIR}/worker_hostname

echo > ${AN_DIR}/nw_external_mk
echo > ${AN_DIR}/joined_results_1K

##
## ANNOTATION DIR
##
cp ${BASE_DATA_PATH}/0_src/${BASE_DATE}/orgs/${GENOME_ID} ${AN_DIR}/orgs/

echo ${BASE_DATE} > ${AN_DIR}/base_date.txt

# Только для исключения этого генома
echo "${GENOME_ID}" > ${AN_DIR}/excl_genomes.txt

1_databases/create_genom_db.py      ${AN_DIR}/orgs                  ${AN_DIR}/genom.db
1_databases/create_gen_desc_db.py   ${AN_DIR}/orgs                  ${AN_DIR}/gen_desc.db
1_databases/create_genes.py         ${AN_DIR}/genom.db              ${AN_DIR}/gen_desc.db       > ${AN_DIR}/genes.txt
1_databases/create_gene_info.py     ${AN_DIR}/orgs                  ${AN_DIR}/genes_info.txt
sort -t$'\t' -k1,1 -o               ${AN_DIR}/genes_info.txt        ${AN_DIR}/genes_info.txt

join -t$'\t' -1 2 -2 1 \
    <(join -t$'\t' -1 2 -2 1 \
        <(cat "${AN_DIR}/genes_info.txt" | cut -f1,2 | sort -t$'\t' -k2,2 -T . -S1G) \
        "${BASE_DATA_PATH}/7_aux/kegg/${BASE_DATE}/ncbi2uniprot.txt" \
        | cut -f2,3 \
        | sort -t$'\t' -k2,2 -T . -S1G \
    ) \
    <( zcat "${BASE_DATA_PATH}/7_aux/gene_ontology/${BASE_DATE}//uniprot2go.txt.gz" ) \
    | cut -f2,3 \
    | sort -t$'\t' -k1,1 -T . -S1G \
    > ${AN_DIR}/genes_go.txt

##
## DATABASES
##

mkdir -p ${TMP_DATA_PATH}/1_databases/

export CUR_DATA_DIR=${TMP_DATA_PATH}/1_databases/

DIRS_FOUND=`ls ${TMP_DATA_PATH}/1_databases/ | wc -l`

if [ "${DIRS_FOUND}" -eq "0" ]; then
    TMP_DIR=${TMP_DATA_PATH}/1_databases/${BASE_DATE}.tmp.$(date +%s)
    mkdir -p ${TMP_DIR}
    cp -r ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/wu_blast_db  ${TMP_DIR}
    cp -r ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genes_go.txt ${TMP_DIR}
    cp -r ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genom.db     ${TMP_DIR}
    if [ ! -d ${TMP_DATA_PATH}/1_databases/${BASE_DATE} ]; then
        mv ${TMP_DIR} ${TMP_DATA_PATH}/1_databases/${BASE_DATE}
    else
        rm -r ${TMP_DIR}
    fi
fi

##
## NW Interpolation
##
export CUR_DATA_DIR=${TMP_DATA_PATH}/3_raw_matrix_nw/simulation/pupy
mkdir -p $CUR_DATA_DIR
cp ${BASE_DATA_PATH}/3_raw_matrix_nw/simulation/pupy/interpolation.txt ${CUR_DATA_DIR}/

##
## NORM MATRIX
##
export CUR_DATA_DIR=${TMP_DATA_PATH}/4_normed_matrix/${BASE_DATE}/wu_blast_nw_excl_${GENOME_ID}
mkdir -p $CUR_DATA_DIR

cp ${BASE_DATA_PATH}/4_normed_matrix/${BASE_DATE}/wu_blast_nw/map_int_gene.txt  ${CUR_DATA_DIR}/
cp ${BASE_DATA_PATH}/4_normed_matrix/${BASE_DATE}/wu_blast_nw/map_int_genom.txt ${CUR_DATA_DIR}/

zcat ${BASE_DATA_PATH}/3_raw_matrix_nw/${BASE_DATE}/wu_blast/results.txt.gz \
    | awk -F'\t' '{print $2"\t"$1"\t"$3}' \
    | 4_normed_matrix/apply_maps.py \
            --genes-map ${CUR_DATA_DIR}/map_int_gene.txt \
            --genomes-map ${CUR_DATA_DIR}/map_int_genom.txt \
            --excl-genomes ${GENOME_ID} \
    | gzip \
    > ${CUR_DATA_DIR}/matrix.txt.gz

export Z_VALUE=5

zcat ${CUR_DATA_DIR}/matrix.txt.gz \
    | awk -F'\t' '{if ($3>='$Z_VALUE') print $1"\t"$2}' \
    | gzip \
    > ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz

rm ${CUR_DATA_DIR}/matrix.txt.gz

zcat ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz \
    | 4_normed_matrix/add_vec_len.py \
    | awk -F'\t' '{print $2"\t"$1}' \
    | sort -t$'\t' -n -k1,1 -S500M -T ${CUR_DATA_DIR} \
    | 4_normed_matrix/create_matrix.py \
    | gzip \
    > ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz

rm ${CUR_DATA_DIR}/matrix_raw_${Z_VALUE}_.txt.gz

zcat ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz \
    | tools/create_bsddb.py ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.db
    
rm ${CUR_DATA_DIR}/matrix_trans_${Z_VALUE}_.txt.gz


##
## Annotate genes
##

mkdir $AN_DIR/src
cat $AN_DIR/genes.txt | split -a3 -l 1 - $AN_DIR/src/

mkdir $AN_DIR/src_faa
ls $AN_DIR/src | xargs -n1 -P1 -I {} bash -c "cat $AN_DIR/src/{} | awk -F'\t' '{print \">\"\$1\"\n\"\$2}' > $AN_DIR/src_faa/{}.faa" 

# full
for i in `seq 1 10`; do
  find $AN_DIR/src_faa/ -type f -name "*.faa" | sort | xargs -n1 -P1 -I {} bash -c "cat {} | 6_annotation/annotate_wu_nw_simple_local.sh $AN_DIR && rm {}"
done

# restrict
# find $AN_DIR/ -maxdepth 1 -type d -name "annot_*" | sort | xargs -n1 -P1 6_annotation/annotate_wu_nw_simple_restrict.sh

# rm -rf ${CUR_DATA_DIR}

9_annotation_analysis/create_results_exact_go.sh $AN_DIR

